package Gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;

public class AboutPanel extends Frame {
	private static AboutPanel ref;
	TextArea textarea;

	private AboutPanel(String filename) {
		super();
		// create TextArea
		textarea = new TextArea("", 24, 80);
		textarea.setFont(new Font("MonoSpaced", Font.PLAIN, 16));
		textarea.setEditable(false);
		this.add("Center", textarea);

		// create bottom panel
		Panel p = new Panel();
		p.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 5));
		this.add(p, "South");

		JButton ok = new JButton("OK");
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		
		add(ok, BorderLayout.SOUTH);
		this.pack();
		
		loadFile(filename);
		setVisible(true);
	}

	private void loadFile(String filename) {
		if ((filename == null) || (filename.length() == 0))
			return;
		File f;
		FileReader in = null;
		// Read and display the file contents. Since we're reading text, we
		// use a FileReader instead of a FileInputStream.
		try {
			f = new File(filename); // Create a file object
			in = new FileReader(f); // And a char stream to read it
			char[] buffer = new char[4096]; // Read 4K characters at a time
			int len; // How many chars read each time
			textarea.setText(""); // Clear the text area
			while ((len = in.read(buffer)) != -1) { // Read a batch of chars
				String s = new String(buffer, 0, len); // Convert to a string
				textarea.append(s); // And display them
			}
			this.setTitle("FileViewer: " + filename); // Set the window title
			textarea.setCaretPosition(0); // Go to start of file
		}
		// Display messages if something goes wrong
		catch (IOException e) {
			textarea.setText(e.getClass().getName() + ": " + e.getMessage());
			this.setTitle("FileViewer: " + filename + ": I/O Exception");
		}
		// Always be sure to close the input stream!
		finally {
			try {
				if (in != null)
					in.close();
			} catch (IOException e) {
			}
		}
	}

	public static AboutPanel getInstance() {
		if (ref == null)
			ref = new AboutPanel("Images\\gameAbout.txt");
		return ref;
	}
}